package com.sch.www.sys.controller;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.sch.www.common.base.BaseController;
import com.sch.www.common.base.ResultBean;
import com.sch.www.common.validator.NotBlankFluentValidator;
import com.sch.www.common.validator.SizeFluentValidator;
import com.sch.www.sys.orm.entity.manual.Resource;
import com.sch.www.sys.services.ResourceServices;
import com.sch.www.sys.services.AccountServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;


@RequestMapping("/v1/resource")
@RestController
public class ResourceController extends BaseController {

    @Autowired
    ResourceServices resourceServices;
    @Autowired
    AccountServices accountServices;

    @RequestMapping("/listAll")
    public ResultBean<List<Resource>> listAll() {
        ResultBean<List<Resource>> resultBean = ResultBean.newInstance();
        List<Resource> resourceList = resourceServices.selectAll();
        return resultBean.setData(resourceList).setSuccess();
    }

    @RequestMapping("/listByActive")
    public ResultBean<List<Resource>> listByActive(@RequestParam Byte active) {
        ResultBean<List<Resource>> resultBean = ResultBean.newInstance();
        List<Resource> resourceList = resourceServices.selectAll().stream().filter(item -> item.getActive() == active).collect(Collectors.toList());
        return resultBean.setData(resourceList).setSuccess();
    }

    @RequestMapping("/listByRoleId")
    public ResultBean<List<Resource>> listByRoleId(@RequestParam String roleId) {
        ResultBean<List<Resource>> resultBean = ResultBean.newInstance();
        List<Resource> resourceList = resourceServices.selectByRoleId(roleId);
        return resultBean.setData(resourceList).setSuccess();
    }


    @RequestMapping("/add")
    public ResultBean add(@RequestBody Resource resource) {
        ResultBean<List<Resource>> resultBean = ResultBean.newInstance();

        ComplexResult complexResult = FluentValidator.checkAll().failOver()
                .on(resource.getName(), new NotBlankFluentValidator("name"))
                .on(resource.getPermission(), new NotBlankFluentValidator("permission"))
                .on(resource.getUrl(), new NotBlankFluentValidator("url"))
                .on(resource.getActive(), new SizeFluentValidator(0, 1, "active"))
                .on(resource.getType(), new SizeFluentValidator(0, 1, "type"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!complexResult.isSuccess()) {
            return resultBean.setError().appendMsg(complexResult);
        }
        resourceServices.insertSelective(resource);
        return resultBean.setSuccess();
    }

    @RequestMapping("/modify")
    public ResultBean modify(@RequestBody Resource resource) {
        ResultBean<List<Resource>> resultBean = ResultBean.newInstance();

        ComplexResult complexResult = FluentValidator.checkAll().failOver()
                .on(resource.getId(), new NotBlankFluentValidator("id"))
                .on(resource.getName(), new NotBlankFluentValidator("name"))
                .on(resource.getPermission(), new NotBlankFluentValidator("permission"))
                .on(resource.getUrl(), new NotBlankFluentValidator("url"))
                .on(resource.getActive(), new SizeFluentValidator(0, 1, "active"))
                .on(resource.getType(), new SizeFluentValidator(0, 1, "type"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!complexResult.isSuccess()) {
            return resultBean.setError().appendMsg(complexResult);
        }
        if (resourceServices.updateByIdSelective(resource)) {
            return resultBean.setSuccess();
        } else {
            return resultBean.setError();
        }

    }



}
